package com.cyt.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.cyt.entity.SPFeiLei;
import com.cyt.entity.XingWenFengLei;
import com.cyt.util.DBManager;
// 新闻分类 基础
public class XingWenFengLeiDaoImpl {
	public List<XingWenFengLei> getXingWenFengLei() {
		List<XingWenFengLei> list = new ArrayList<XingWenFengLei>();
		Statement st = null;
		ResultSet rs = null;
		// 1.建立连接
		Connection conn = DBManager.getConnection();
		// 2.创建Statement执行对象(用来执行sql语句)
		try {// try 正常执行
			st = conn.createStatement();
			// 3.sql语句
			String sql = "SELECT * FROM xingwenfenglei";
			// 4.执行sql语句,返回结果集
			rs = st.executeQuery(sql);
			// 5. 遍历结果集
			while (rs.next()) {
				int xWFeiLeiId = rs.getInt("xWFeiLeiId");
				String xWFengLeiName = rs.getString("xWFengLeiName");
				XingWenFengLei xw = new XingWenFengLei(xWFeiLeiId, xWFengLeiName);// 创建用户对象
				list.add(xw);// 把user对象存入list中
			}
			return list;// 正常情况返回集合

		} catch (SQLException e) {// catch 异常执行
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;// 异常返回空
		} finally {
			DBManager.closeAll(conn, st, rs);
		}

	}
	
	
	/**
	 * 分页查询
	 * @param page
	 * @param size
	 * @return
	 */
	public List<XingWenFengLei> getXingWenFengLeiPage(int page, int size) {
		List<XingWenFengLei> list = new ArrayList<XingWenFengLei>();
		Statement st = null;
		ResultSet rs = null;
		// 1.建立连接
		Connection conn = DBManager.getConnection();
		// 2.创建Statement执行对象(用来执行sql语句)
		try {// try 正常执行
			st = conn.createStatement();
			// 3.sql语句
			String sql = "select * FROM xingwenfenglei LIMIT "+(page-1)*size+","+size;
			System.out.println(sql);

			// 4.执行sql语句,返回结果集
			rs = st.executeQuery(sql);
			// 5. 遍历结果集
			while (rs.next()) {
				int xWFeiLeiId = rs.getInt("xWFeiLeiId");
				String xWFengLeiName = rs.getString("xWFengLeiName");
				XingWenFengLei xw = new XingWenFengLei(xWFeiLeiId, xWFengLeiName);// 创建用户对象
				list.add(xw);// 把user对象存入list中
			}
			return list;// 正常情况返回集合

		} catch (SQLException e) {// catch 异常执行
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;// 异常返回空
		} finally {
			DBManager.closeAll(conn, st, rs);
		}

	}
	/**
	 * 根据id删除资讯分类
	 * @param ziXunId要删除的资讯分类的id
	 * @return返回数据库受影响行数
	 */
	public int shanZiXunFL(int ziXunId) {
		return DBManager.executeUpdate("DELETE FROM xingwenfenglei where xWFeiLeiId="+ziXunId);
	}
	
	
	/**
	 * 通过id主键删除
     * 批量删除
	 * @param 
	 * @return
	 */
	public int shanZiXunFLS(String ids) {
		String sql = "delete from xingwenfenglei where xWFeiLeiId in("+ids+")";
		System.out.println(sql);
		return DBManager.executeUpdate(sql);
	}
	
	/**添加资讯分类
	 * @param zx
	 * @return 返回数据库受影响行数
	 */
	public int addZiXunFL(XingWenFengLei zx ) {
		return DBManager.executeUpdate("INSERT into xingwenfenglei VALUES(null,'"+zx.getxWFengLeiName()+"')");
	}
	
	
	
	
	/**
	 * 查询新闻类别的名字,以供添加时比对数据库是否存在此类别
	 * @param name
	 * @return
	 */
	public List<XingWenFengLei> getZiXunFLName(String name){
		List<XingWenFengLei> list = new ArrayList<XingWenFengLei>();
		Statement st = null;
		ResultSet rs = null;
		// 1.建立连接
		Connection conn = DBManager.getConnection();
		// 2.创建Statement执行对象(用来执行sql语句)
		try {// try 正常执行
			st = conn.createStatement();
			// 3.sql语句
			String sql = "SELECT * FROM xingwenfenglei WHERE xWFengLeiName='"+name+"'";
			// 4.执行sql语句,返回结果集
			rs = st.executeQuery(sql);
			// 5. 遍历结果集
			while (rs.next()) {
				int xWFeiLeiId = rs.getInt("xWFeiLeiId");
				String xWFengLeiName = rs.getString("xWFengLeiName");
				XingWenFengLei xw = new XingWenFengLei(xWFeiLeiId, xWFengLeiName);// 创建用户对象
				list.add(xw);
			}
			return list;// 正常情况返回集合

		} catch (SQLException e) {// catch 异常执行
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;// 异常返回空
		} finally {
			DBManager.closeAll(conn, st, rs);
		}
		
	}
	
	/**
	 * 根据id查新闻类别信息,以供修改的数据回显
	 * @param xwflId
	 * @return
	 */
	public XingWenFengLei chaXingWenFengLeiId(int xwflId) {
		XingWenFengLei xw=null;
		Statement st = null;
		ResultSet rs = null;
		// 1.建立连接
		Connection conn = DBManager.getConnection();
		// 2.创建Statement执行对象(用来执行sql语句)
		try {// try 正常执行
			st = conn.createStatement();
			String sql = "SELECT * FROM xingwenfenglei WHERE xWFeiLeiId="+xwflId;
			rs = st.executeQuery(sql);
			while (rs.next()) {
				int xWFeiLeiId = rs.getInt("xWFeiLeiId");
				String xWFengLeiName = rs.getString("xWFengLeiName");
				 xw = new XingWenFengLei(xWFeiLeiId, xWFengLeiName);
				
			}
			return xw;

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn, st, rs);
		}

	}
	
	/**
	 * 修改新闻类别  根据id
	 * @param xw
	 * @return
	 */
	public int xiuGaiZiXunFL(XingWenFengLei xw ) {
		String sql="UPDATE xingwenfenglei set xWFengLeiName='"+xw.getxWFengLeiName()+"' where xWFeiLeiId="+xw.getxWFeiLeiId();
		System.out.println(sql);
		return DBManager.executeUpdate(sql);
	}
	
	
}
